package com.tanhua.sso.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tanhua.sso.config.AliyunSMSConfig;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.Map;

@Slf4j
@Service
public class Sendsms {

    @Autowired
    RedisTemplate<String,String>redisTemplate;
    /**
     * 返回的参数类型就是错误请求类型
     * 如果返回null相当于200发送成功，如果里面有值，相当于返回失败404、500
     * @param phone
     * @return
     */


    public ErrorResult sendCheckCode(String phone) {
        String redisKey = "CHECK_CODE_" + phone;
        //通过redis中的hasKey方法先判断该手机号发送的验证码是否还生效
        if (this.redisTemplate.hasKey(redisKey)){
            String msg="上一次发送的验证码还未失效！";
            return ErrorResult.builder().errCode("000001").errMessage(msg).build();
        }

//        String s = this.sendSms(phone);
        String s="123456";
        //通过发送消息得到的返回值，如果为空，那么就打印发送失败
        if (StringUtils.isEmpty(s)){
            String msg1="发送验证码失败！";
            return ErrorResult.builder().errCode("000000").errMessage(msg1).build();
        }
        //如果上面两个都没执行，那么就是发送成功了，同时将数据保存到redis中，并返回null
         this.redisTemplate.opsForValue().set(redisKey,s, Duration.ofMinutes(5));
        return null;
    }
}
